package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductCateConverter;
import com.cskaoyan.shopping.dal.entitys.*;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.ProductCateDto;
import com.cskaoyan.shopping.service.IHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * @author wei
 * @since 2022/09/20 21:02
 */
@Service
public class IHomeServiceImpl implements IHomeService {
    @Autowired
    ContentConverter contentConverter;
    @Autowired
    ProductCateConverter productCateConverter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ItemMapper itemMapper;
    @Override
    public Set<PanelDto> homepage() {

        HashSet<PanelDto> hashSet = new HashSet<>();
        List<Panel> panels = panelMapper.selectAll();
        for (Panel panel : panels) {
            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
            PanelDto panelDto = contentConverter.panel2Dto(panel);
            List<PanelContentItemDto> panelContentItemDtos = contentConverter.panelContentItem2Dto(panelContentItems);
            panelDto.setPanelContentItems(panelContentItemDtos);
            hashSet.add(panelDto);
        }
        return hashSet;

    }

    @Override
    public List<PanelContentDto> navigation() {

        Example example = new Example(PanelContent.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("panelId",0);
        List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
        List<PanelContentDto> panelContentDtos = contentConverter.panelContents2Dto(panelContents);
        return panelContentDtos;
    }

    @Override
    public List<ProductCateDto> categories() {
        List<ItemCat> itemCats = itemCatMapper.selectAll();
        List<ProductCateDto> productCateDtos = productCateConverter.items2Dto(itemCats);
        return productCateDtos;
    }

    @Override
    public PanelDto queryProductRecommend(Integer panelId) {
        Panel panel = panelMapper.selectByPrimaryKey(panelId);
        PanelDto panelDto = contentConverter.panel2Dto(panel);

        Example example = new Example(PanelContent.class);
        example.createCriteria().andEqualTo("panelId",panelId);
        List<PanelContent> panelContents = panelContentMapper.selectByExample(example);

        LinkedList<PanelContentItemDto> panelContentItemDtos = new LinkedList<>();
        for (PanelContent panelContent : panelContents) {
            Long productId = panelContent.getProductId();
            //通过productId去item表中搜索其他属性
            Item item = itemMapper.selectByPrimaryKey(productId);

            PanelContentItemDto panelContentItemDto = contentConverter.panelContentItemDto(panelContent);
            panelContentItemDto.setCreated(item.getCreated());
            panelContentItemDto.setUpdated(item.getUpdated());
            panelContentItemDto.setProductName(item.getTitle());
            panelContentItemDto.setSalePrice(item.getPrice());
            panelContentItemDto.setSubTitle(item.getSell_point());

            panelContentItemDtos.add(panelContentItemDto);
        }

        panelDto.setPanelContentItems(panelContentItemDtos);

        return panelDto;
    }
}

